package com.example.springboot.config.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class PathAllowFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化操作（如果需要）
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;

        // 获取请求的 URI
        String path = httpRequest.getRequestURI();

        // 检查是否是需要放行的路径
        if (shouldAllowAccess(path)) {
            chain.doFilter(request, response); // 放行
        } else {
            chain.doFilter(request, response); // 继续其它过滤器的执行
        }
    }

    @Override
    public void destroy() {
        // 销毁操作（如果需要）
    }

    private boolean shouldAllowAccess(String path) {
        return path.equals("/metAlarm/getMetAlarmInfoByUser") 
            || path.equals("/user/adminLogin")
            || path.equals("/user/loginAndBind")
            || path.equals("/user/login")
            || path.equals("/user/hello")
            || path.equals("/user/register")
            || path.matches(".*/export")
            || path.matches(".*/import")
            || path.matches("/file/.*");
    }
}
